package com.wframe.project.former.web.faretemp;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import com.frame.database.JdataBean;
import com.frame.pageform.DataPageForm;
import com.frame.pageform.annotation.Login;
import com.wframe.project.former.dataservices.faretemp.FaretempService;

@Login
public class FaretempPage extends DataPageForm{

	private JdataBean bean;
	private FaretempService service=FaretempService.getIntance();

	@Override
	public String execute() throws Exception {
		setResultList_(service.queryTemp());
		return "/webadmin/faretemp/list.jsp";
	}
	
	@Override
	public String show() throws Exception {
		if(bean==null){
			bean=new JdataBean();
			bean.put("type", 1);
			bean.put("way", 1);
		}else{
			bean=service.getBean(bean.getString("id"));
			setResultList_(service.queryAreas(bean.getString("id")));
		}
		JdataBean defaultbean=service.getDefault();
		if(defaultbean!=null){
			bean.put("startnum_default", defaultbean.get("startnum"));
			bean.put("startmoney_default", defaultbean.get("startmoney"));
			bean.put("nextnum_default", defaultbean.get("nextnum"));
			bean.put("nextmoney_default", defaultbean.get("nextmoney"));
		}
		return "/webadmin/faretemp/edit.jsp";
	}

	@Override
	public String toDel() throws Exception {
		// TODO Auto-generated method stub
		service.delTemp(bean.getString("id"));
		return getJsonObject().toString();
	}

	@Override
	public boolean toUpdate() throws Exception {
		String[] areas=null;
		if(bean.get("areas")!=null){
			if(bean.get("areas") instanceof String)areas=new String[]{bean.getString("areas")};
			else areas=(String[])bean.get("areas");
		}
		JdataBean defaultbean=new JdataBean();
		defaultbean.put("startnum", bean.getString("startnum_default"));
		defaultbean.put("startmoney", bean.getString("startmoney_default"));
		defaultbean.put("nextnum", bean.getString("nextnum_default"));
		defaultbean.put("nextmoney", bean.getString("nextmoney_default"));
		defaultbean.put("way", bean.getString("way"));
		service.saveDefault(defaultbean);
		if(areas!=null){
			JdataBean temp=new JdataBean();
			temp.put("id", bean.getString("id"));
			temp.put("name", bean.getString("name"));
			temp.put("startnum", bean.getString("startnum"));
			temp.put("startmoney", bean.getString("startmoney"));
			temp.put("nextnum", bean.getString("nextnum"));
			temp.put("nextmoney", bean.getString("nextmoney"));
			temp.put("way", bean.getString("way"));
			List<JdataBean> arebeans=new ArrayList<JdataBean>();
			for(String area:areas){
				String[] ss=area.split(",",2);
				JdataBean arebean=new JdataBean();
				arebean.put("province", ss[0]);
				if(StringUtils.isNotBlank(ss[1])){
					arebean.put("city", ss[1]);
					if(!arebean.getString("city").endsWith("市") && !arebean.getString("city").endsWith("区"))arebean.put("city", arebean.getString("city")+"市");
				}
				arebeans.add(arebean);
			}
			String tempid=service.saveTemp(temp);
			service.saveArea(tempid, arebeans);
		}
		getJsonObject().getJsonPage().showOkMsg("保存成功");
		getJsonObject().getJsonPage().closeWin("ok");
		return false;
	}

	public JdataBean getBean() {
		return bean;
	}

	public void setBean(JdataBean bean) {
		this.bean = bean;
	}

}
